import { Ref } from "vue";

export function useLoadingFn<T extends Array<unknown>, R>(fn : (...args : T) => Promise<R>, loading : Ref<boolean>) {
	function wrapper(this, ...args : T) {
		loading.value = true;
		return new Promise<R>((resolve, reject) =>
			Promise.resolve(fn.apply(this, args)).then(resolve).catch(reject)
		).finally(() => loading.value = false);
	}
	return wrapper;
}